<?php

namespace app\admin\controller\mall;


use app\admin\service\TriggerService;
use app\api\model\mysql\MallSetting;
use app\common\controller\AdminController;
use think\App;

class Setting extends AdminController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new MallSetting();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $config =$this->model->select()->toArray();
        $configs =array();
        foreach ($config as $k=>$v){
            switch ($v['name']){
                case 'mobile':
                    $configs['mobile'] =$v['option_value'];
                    break;
                case 'balance':
                    $configs['balance'] =$v['option_value'];
                    break;
                case 'withdrawal':
                    $configs['withdrawal'] =$v['option_value'];
                    break;
                case 'rule':
                    $configs['rule'] =$v['option_value'];
                    break;
                case 'teamset':
                    $configs['teamset'] =$v['option_value'];
                    break;
                case 'shippingpersent':
                    $configs['shippingpersent'] =$v['option_value'];
                    break;
                case 'is_show':
                    $configs['is_show'] =$v['option_value'];
                    break;
                case 'isopen':
                    $configs['isopen'] =$v['option_value'];
                    break;
                default:
                    break;
            }
        }

        $this->assign('configs',$configs);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="保存")
     */
    public function save()
    {
        $this->checkPostRequest();
        $post = $this->request->post();

        try {
            foreach ($post as $key => $val) {
                if($key=='rule'){
                    $val =htmlspecialchars_decode($val);
                }

                $this->model
                    ->where('name', $key)
                    ->update([
                        'option_value' => $val,
                    ]);
            }
        } catch (\Exception $e) {
            $this->error('保存失败');
        }
        $this->success('保存成功');
    }
}